package com.jtech.together.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.jtech.together.manager.UserManager;
import com.jtech.together.model.UserModel;
import com.jtech.together.util.BmobUtil;

import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 位置服务
 * Created by wuxubaiyang on 2017/5/11.
 */

public class LocationService extends Service implements AMapLocationListener {
    //定位频率
    private static final long LOCATION_INTERVAL = 1000 * 60 * 30;

    private AMapLocationClient aMapLocationClient;

    @Override
    public void onCreate() {
        super.onCreate();
        //实例化位置获取
        this.aMapLocationClient = new AMapLocationClient(getApplicationContext());
        //设置监听
        this.aMapLocationClient.setLocationListener(this);
        //设置配置参数
        this.aMapLocationClient.setLocationOption(getOption());
        //开启定位
        this.aMapLocationClient.startLocation();
    }

    /**
     * 获取配置对象
     *
     * @return
     */
    private AMapLocationClientOption getOption() {
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//定位模式为高精度
        option.setInterval(LOCATION_INTERVAL);//设置定位频率
        option.setNeedAddress(true);//是否返回地理位置信息
        option.setLocationCacheEnable(false);//关闭缓存机制
        return option;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation.getErrorCode() == 0) {//定位成功
            //更新用户信息
            updateUserInfo(new BmobGeoPoint(aMapLocation.getLongitude(), aMapLocation.getLatitude()), aMapLocation.getAddress());
        } else {
            Log.i("locationService", "onLocationChanged: " + aMapLocation.getErrorInfo());
        }
    }

    /**
     * 更新用户信息
     *
     * @param location
     * @param address
     */
    private void updateUserInfo(BmobGeoPoint location, String address) {
        UserModel userModel = UserManager.getCurrentUser();
        userModel.setAddress(address);
        userModel.setLocation(location);
        userModel.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (null == e) {
                    Log.i("locationService", "done: 位置更新成功");
                } else {
                    Log.i("locationService", "done: 位置更新失败" + BmobUtil.getErrorMessage(e.getErrorCode()));
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //反注册定位监听
        aMapLocationClient.unRegisterLocationListener(this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}